<?php

namespace app\models;

use Yii;
use yii\base\Model;

/**
 * Rbac权限表单
 * Class RbacPermission
 * @package app\models
 *
 * @property string $parent 上级权限名称
 * @property string $name 权限名称
 * @property string $description 权限说明
 */
class RbacPermission extends Model
{
    public $parent;
    public $name;
    public $description;

    /**
     * @inheritdoc
     */
    public function rules()
    {
        return [
            [['parent', 'name', 'description'], 'required'],
            ['name', function() {
                $authManager = Yii::$app->authManager;
                if (!empty($authManager->getPermission($this->name))) {
                    $this->addError('name', '已经存在这个权限了。');
                    return false;
                }
                return true;
            }],
        ];
    }

    /**
     * 保存新权限
     * @return boolean
     */
    public function save()
    {
        if (!$this->validate()) {
            return false;
        }
        $authManager = Yii::$app->authManager;
        $permission = $authManager->createPermission($this->name);
        $permission->description = $this->description;
        if (!$authManager->add($permission)) {
            $this->addError('name', '无法保存权限信息。');
            return false;
        }
        $parent = $authManager->getPermission($this->parent);
        if (empty($parent)) {
            $this->addError('parent', '没有找到上级权限。');
            return false;
        }
        if (!$authManager->addChild($parent, $permission)) {
            $this->addError('parent', '无法设定上下级关系。');
            return false;
        }
        return true;
    }
}
